<template>
 	<view class="Page">
 		<view class="title">已扫码列表</view>
 		<view class="noData" v-show="list.length==0">当前无扫码数据</view>
 		<view v-show="list.length!=0" class="is_show_box">
 			<view>
 				<view class="data_item">
 
 						<view class="card-left">
 							<view><text class="gray">物料名称：</text>{{list[0]?.spare_manage__name}}</view>
 							<view><text class="gray">物料编码：</text>{{list[0]?.spare_manage__code}}</view>
 							<view style="font-weight:600 ;">盘盈数量({{list[0]?.spare_manage__unit__name}})：<text style="color: red;"> {{list[0]?.num}}</text><image class="num-icon" src="../../static/num_update.svg" @click="num_update(list[0])"></image></view>
 							<view><text class="gray">规格：</text>{{list[0]?.spare_manage__specs }} {{' '}}<text class="gray">型号：</text>{{list[0]?.spare_manage__model}}</view>
 							<view><text class="gray">库存属性：</text>{{list[0]?.now_spare_status__name }} {{' '}}<text class="gray">流水号：</text>{{list[0]?.no}}</view>
							<view><text class="gray">库存位置：</text>{{list[0]?.store_place__flag}}</view>
 						</view>
 				</view>
 				
 				
 			</view>	
 
 		</view>
		<view class="title">备注</view>
		<textarea placeholder="请输入备注" v-model="bz"></textarea>
		<uni-popup ref="is_oldEnter" type="dialog">
			<uni-popup-dialog type="success" cancelText="取消" confirmText="确定" title="通知" content="是否确定盘盈入库" @confirm="oldEnterConfirm"
				@close="isMoveClose"></uni-popup-dialog>
		</uni-popup>
 		<view class="kongbai">
 			
 		</view>
 	</view>
	<uni-popup ref="receive_popup" type="dialog" >
		<view class="dialog_center">
			<uni-popup-dialog 
				mode="input" 
				message="成功消息" 
				:duration="2000" 
				:before-close="true" 
				@confirm="reclose_confirm" 
				title="盘盈" 
				placeholder="请输入盘盈数量"
				v-model="num">
			</uni-popup-dialog>
		</view>
		
	</uni-popup>
	<view class="input_goal" v-if="!list[0]?.store_place__flag"><label style="font-size: 14px;">目标库位：</label><input v-model="location" style="font-size: 14px;"/><image src="../../static/scan_icon.svg" mode="aspectFit" @click="scan_goal"></image></view>
	<view class="buttonBox">
		<button size="default" type="primary"  @click="oldEnter">盘盈入库</button>
		<button size="default" type="default"  @click="scan1" class="scanButton">扫码</button>
	</view>
 </template>
 
 <script>
 	export default {
 		data() {
 			return {
 				id:123,
 				// is_show:false,
 				code:1234,
 				// 二维码查询出的码
 				qr_code:"",
 				list:[],
 				return_data:[],
				num:0,
 				org_id:34,
				choosedID:'',
				location:'',
				if_scan:false,
				is_find:false,
				bz:'',
				
 			}
 		},
		watch: {
		    location: {
		        handler(oldValue,newVal) {
					console.log(this.if_scan)
					if(this.if_scan){
						this.if_scan=false;
					}
					else{
						this.choosedID='';
					}
		        }
		    }
		},
		computed:{
			getNowFormatDate() {
			  let date = new Date(),
			    year = date.getFullYear(), //获取完整的年份(4位)
			    month = date.getMonth() + 1, //获取当前月份(0-11,0代表1月)
			    strDate = date.getDate() // 获取当前日(1-31)
			  if (month < 10) month = `0${month}` // 如果月份是个位数，在前面补0
			  if (strDate < 10) strDate = `0${strDate}` // 如果日是个位数，在前面补0
			 
			  return `${year}-${month}-${strDate}`
			}
		},
 		onLoad() {
 			this.scan1();
 		},
 		methods: {
 			scan1(){
 				let that=this;
 				uni.scanCode({
 					scanType: ['qrCode'],
 					success: function (res) {
 						console.log(res.result)
 						let strArray=res.result.split(',')
 						console.log(strArray)
 						if(strArray[0][5]!='d'){
 							console.log('扫的码不正确')
 							// that.$refs.mabucunzai.open('center')
							that.$root.message('fail','扫的码不是本系统的码')
 						}
 						else{
 							console.log('扫的码正确')
 							that.qr_code=strArray[0].slice(12);
 							that.org_id=strArray[2].slice(7);
 							console.log(that.qr_code);
 							console.log(that.org_id);
								that.$root.http('http://113.15.231.21:180/storage_manage2/ding_app/query_spare_device_list',
									{
										org_id:that.org_id, 
										ids:that.qr_code, 
									}, 
									{
									  	sessionid:that.$root.sessionID
									})
									.then((res) => {
										let fun=function(){
											if(res.data.result&&res.data.result.list&&res.data.result.list.length>0){
												console.log("存在");
												console.log(res.data.result.list[0]);
											
												that.list[0]=res.data.result.list[0];
												// 返回服务端的数组
												
												that.return_data=[{
													spare_status_flag: res.data.result.list[0]?.now_spare_status__flag ? res.data.result.list[0]?.now_spare_status__flag:'new', // 操作流水号的备件状态，有获取，没有默认“new”新件
													spare_device_id: res.data.result.list[0].id,
													num: res.data.result.list[0].num,   
												}]

											}
											else{
												console.log("不存在");
												that.$root.message('fail','扫的码id不存在')
											}
										}
										that.$root.judge(res,fun);
									})
 						}
 					}
 				});	
 				
 			},
			num_update(item){
				// this.num=item.num;
				this.$refs.receive_popup.open('center')
				console.log('num'+this.num)
			},
			reclose_confirm(num) {
				this.list[0].num=num;
				this.return_data[0].num=num;
				this.$refs.receive_popup.close();
			},
			scan_goal(){
				let that=this;
				uni.scanCode({
					scanType: ['qrCode'],
					success: function (res) {
						console.log(res.result)
						that.if_scan=true;
						let strArray=res.result.split(',')
						console.log(strArray)
						if(strArray[0][5]!='g'){
							that.$root.message('fail','扫的码不是位置码')
						}
						else{
							that.choosedID=strArray[0].slice(14);
							that.location=strArray[1].slice(5);
							console.log(that.choosedID);
							console.log('location'+that.location);
						}
				 
					},
				});
			},
			oldEnter(){
				if(this.return_data.length>0&&this.list.length>0){
					if(this.return_data[0].num>0){
						if(!this.list[0]?.store_place__flag&&this.choosedID==''){
							let that=this;
							that.$root.http('http://113.15.231.21:180/storage_manage2/ding_app/query_storage_place_info_list',
								{
									org_id:34,
								}, 
								{
								  	sessionid:that.$root.sessionID
								})
								.then((res) => {
									let fun=function(){
										that.storageList=res.data.result.list;
										// console.log(that.storageList[0].name);
										for(var item in res.data.result.list){
											if(res.data.result.list[item].name==that.location){
												that.choosedID=res.data.result.list[item].id;
												that.is_find=true;
											}
										}
										if(!that.is_find){
											that.$root.message('fail','目标位置错误,请选择正确的目标位置')
										}
										else{
											that.$refs.is_oldEnter.open('center')
										}
									}
									that.$root.judge(res,fun);
								})
						}
						else{
							this.$refs.is_oldEnter.open('center')
						}
					}
					else{
						this.$root.message('fail','盘盈数量为0，请输入正确的盘盈数量')
					}
				}
				else{
					this.$root.message('fail','请先扫码添加物料')
				}
			},
			oldEnterConfirm(){
				let that=this;
				that.$root.http('http://113.15.231.21:180/storage_manage2/ding_app/create_spare_record_by_normal',
					{
						org_id:that.org_id,               // 组织ID
						record_type_flag: 'pan_in',   // 业务类型，这里写死
					   store_place_id:that.list[0]?.store_place__flag ? that.list[0]?.store_place_id : that.choosedID,       // 目标库位
					   date:that.getNowFormatDate,        // 领用时间，默认当天
					   data:JSON.stringify(that.return_data),
					   bz:that.bz
					}, 
					{
						sessionid:that.$root.sessionID
					})
					.then((res) => {
						let fun=function(){
							that.list=[];
							that.return_data=[];
						}
						that.$root.judge(res,fun);
					})
			},
			
 		}
 	}
 </script>
 
 <style scoped>
 	.Page{
 		width: 100%;
 		font-size: 14px;
  		background-color: #F6F6F6;
 		display: flex;
 		flex-direction: column;
 		/* align-items: center; */
		padding-left: 10px;
		padding-right: 10px;
 		
 	}
 	.title{
 		font-size: 16px;
 		margin-top: 10px;
 		font-weight: 600;
		border-left: 2px solid dodgerblue;
		padding-left: 8px ;
 	}
 	.noData{
 		margin-top: 15px;
 	}
 	textarea{
		margin-top: 15px;
		width: 93%;
		font-size: 14px;
	}
 
 	.data_item{
 		margin-top: 12px;
 		width: 90%;
 		background-color: white;
 		border-radius: 5px;
 		font-size: 14px;
 		padding-left: 10px;
 		padding-bottom: 5px;
 		display: flex;
 		flex-direction: row;
 		justify-content: space-between;
 		
 	}
 	.card-left view{
 		margin-top: 5px;
 	}
 	.gray{
 		color: #93A1A1;
 	}
 	.num-icon{
 		width: 16px;
 		height: 16px;
 	}
 	.dialog_center{
 		display: flex;
 		flex-direction: row;
 		justify-content: center;
 		align-items: center;
 	}
 	.is_show_box{
 		width: 100%;
 	}
	.input_goal{
		position: fixed;
		bottom: 49px;
		width: 100%;
		height: 40px;
		background-color: white;
		line-height: 40px;
		display: flex;
		justify-content: space-around;
		align-items: center;
	}
	.input_goal input{
		border: 1px solid gainsboro;
		width: 55%;
		height: 30px;
	}
	.input_goal image{
		width: 12%;
		height: 40px;
	}
 	.kongbai{
 		width: 100%;
 		height: 60px;
 	}
 	.buttonBox{
 		width: 100%;
 		position: fixed;
 		bottom: 0%;
 		display: flex;
 		flex-direction: row;
 		justify-content: space-around;
 		align-items: center;
 	}
 	.buttonBox button{
 		width: 50%;
 	}
 	.scanButton{
 		background-color: #42B983;
 		/* background-color: #41A863; */
 		color: white;
 		border: 1px solid #42B983;
 	}
 </style>
 